package com.hainatong.shiphelper.module.handlejob.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentHandoverDetailBinding;
import com.hainatong.shiphelper.mainhome.adapter.ExamineStepAdapter;
import com.hainatong.shiphelper.mainhome.entity.ExamineStepBean;
import com.hainatong.shiphelper.module.handlejob.HandleJobDetailActivity;
import com.hainatong.shiphelper.module.handlejob.entity.HandleInfoBean;
import com.hainatong.shiphelper.module.handlejob.viewmodel.HandleJobViewModel;
import com.hainatong.shiphelper.module.holiday.entity.AuditProcessBean;
import com.hainatong.shiphelper.utils.GsonUtil;

import android.os.Bundle;
import android.view.View;

/**
 * 提交的 交接任务详情
 */
public class HandleTaskDetailFragment extends MvvmLazyFragment<FragmentHandoverDetailBinding, HandleJobViewModel>
{
    private static String HANDLE_TASK_TAG = "HandleTaskDetailFragment";

    private HandleInfoBean handleInfoBean;

    private ExamineStepAdapter adapter;

    private List<ExamineStepBean> stepList;

    private List<AuditProcessBean>  auditProcessList;

    public static HandleTaskDetailFragment newInstance(String typeCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( HANDLE_TASK_TAG , typeCode );
        HandleTaskDetailFragment applyFragment = new HandleTaskDetailFragment();
        applyFragment.setArguments( bundle );
        return applyFragment;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        String type = "";
        if(  args != null )
        {
            type = args.getString(HANDLE_TASK_TAG , "");
        }

        if( !StringUtils.isEmpty( type ))
        {
            handleInfoBean = GsonUtil.json2Obj( type,HandleInfoBean.class );
        }
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_handover_detail;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {

        adapter = new ExamineStepAdapter();
        stepList = new ArrayList<>();

        viewDataBinding.rlHandoverLogLayout.setOnClickListener( v -> {
            //
            ToastUtils.showShort( "提交的交接" );
            startActivity( new Intent( getActivity(), HandleJobDetailActivity.class ));
            //ComFragmentActivity.startViewByParam( getActivity(), Constants.VIEW_USER_VACATION_HISTORY , vacationBean.getUser_id() );
        } );


    }

    @Override
    protected void lazyLoad()
    {
        if(ObjectUtils.isNotEmpty( handleInfoBean ))
        {
           viewDataBinding.setHandleTask( handleInfoBean );
           viewDataBinding.executePendingBindings();
        }

        // 审批 流程
        viewModel.getAuditList().observe( this ,result -> {
            if( result.isSuccess() )
            {
                if( result.getData() != null )
                {
                    auditProcessList = result.getData();
                    for(int i=0; i < auditProcessList.size() ; i++ )
                    {
                        AuditProcessBean auditTemp = new AuditProcessBean();
                        if( i ==  auditProcessList.size()-1)
                        {
                            ExamineStepBean step3 = new ExamineStepBean();
                            step3.setStatus( ExamineStepBean.StepStatus.END );
                            step3.setActiveTime("时间:"+ auditTemp.getAudit_time() );
                            step3.setStepName("user:"+ auditTemp.getAudit_name() );
                            if( auditTemp.getAudit_status() == "1" )
                            {
                                step3.setSubName("（已同意）");
                            }
                            else
                            {
                                step3.setSubName("（待审核）");
                            }
                            stepList.add( step3 );
                        }
                        else
                        {
                            ExamineStepBean step2 = new ExamineStepBean();
                            step2.setStatus( ExamineStepBean.StepStatus.ACTIVE );
                            step2.setActiveTime( "时间:"+auditTemp.getAudit_time() );
                            step2.setStepName("user:"+auditTemp.getAudit_name() );
                            if( auditTemp.getAudit_status() == "1" )
                            {
                                step2.setSubName("（已同意）");
                            }
                            else
                            {
                                step2.setSubName("（待审核）");
                            }
                            stepList.add( step2 );
                        }
                    }
                }
                adapter.setList( stepList );
            }
            else
            {

            }
        } );

    }
}
